package com.jt.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jt.admin.common.utils.PageUtils;
import com.jt.admin.common.utils.Query;

import com.jt.admin.domain.mapper.SysMenuMapper;
import com.jt.admin.domain.entity.SysMenuEntity;
import com.jt.admin.service.SysMenuService;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements SysMenuService {
    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysMenuEntity> page = this.page(
                new Query<SysMenuEntity>().getPage(params),
                new QueryWrapper<SysMenuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Set<String> getMenuPerMissionByUserId(Long userId) {
        Set<String> perms=new HashSet<>();
        if (userId != null && 1L == userId){
            perms.add("*:*:*");
        }else {
            List<String> listPerms = sysMenuMapper.selectMenuPermsByUserId(userId);
            listPerms.forEach(item->{
                if (StrUtil.isNotEmpty(item)){
                    perms.addAll(Arrays.asList(item.trim().split(",")));
                }
            });
        }
        return perms;
    }

}